﻿/*****************************************
 * 
 *  copyright (c) by ylshen
 * 
 *  author : ylshen (syl.lucky@gmail.com)
 * 
 **************************************** */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace JYStockAnalyser.Model
{
    /// <summary>
    /// 年季度
    /// </summary>
    [Serializable]
    public class YearQuarter : IComparable<YearQuarter>
    {
        int m_year;
        int m_quarter;

        public YearQuarter()
        {
        }

        public YearQuarter(int year, int quarter)
        {
            m_year = year;
            m_quarter = quarter;
        }

        /// <summary>
        /// 年
        /// </summary>
        public int Year
        {
            get { return m_year; }
            set { m_year = value; }
        }

        /// <summary>
        /// 季度
        /// </summary>
        public int Quarter
        {
            get { return m_quarter; }
            set { m_quarter = value; }
        }

        public const int NULL_QUARTER = -1;

        public override string ToString()
        {
            if (!this.Quarter.Equals(NULL_QUARTER))
            {
                return string.Format("{0}年 {1}季度", Year, Quarter);
            }
            else
            {
                return string.Format("{0}年", Year);
            }
        }

        public virtual string GetTimeSimpleString()
        {
            if (this.Quarter.Equals(NULL_QUARTER))
            {
                return this.Year.ToString().Substring(2, 2);
            }
            else
            {
                return this.Year.ToString().Substring(2, 2) + "-" + this.Quarter.ToString() + "季";
            }
        }

        /// <summary>
        /// 返回指定开始季度到结束季度之间的所有季度
        /// </summary>
        /// <param name="beignQuarter"></param>
        /// <param name="endQuarter"></param>
        /// <returns></returns>
        public static List<YearQuarter> GetCoveredYearQuarters(YearQuarter beignQuarter, YearQuarter endQuarter)
        {
            List<YearQuarter> quarters = new List<YearQuarter>();
            for (int year = beignQuarter.Year; year <= endQuarter.Year; year++)
            {
                for (int quarter = 1; quarter <= 4; quarter++)
                {
                    if (year == beignQuarter.Year && quarter < beignQuarter.Quarter)
                    { continue; }
                    if (year == endQuarter.Year && quarter > endQuarter.Quarter)
                    { break; }

                    quarters.Add(new YearQuarter(year, quarter));
                }
            }
            return quarters;
        }


        #region IComparable<YearQuarter> Members

        public int CompareTo(YearQuarter other)
        {
            if (this.Year == other.Year)
            {
                return this.Quarter.CompareTo(other.Quarter);
            }
            else
            {
                return this.Year.CompareTo(other.Year);
            }
        }

        #endregion
    }
}
